package com.zhilianbao.leyaogo.ui.fragment.goodscategory;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bql.convenientlog.CLog;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.category.CategoryInfo;
import com.zhilianbao.leyaogo.ui.adapter.goodscategory.CategoryAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.BaseOkHttpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 商品分类页面左边的分类部分
 * Created by gyt on 16/11/1.
 */
public class CategoryListFragment extends BaseOkHttpFragment {
    private static final String ARG_CATEGORY        = "arg_category";
    private static final String SAVE_STATE_POSITION = "save_state_position";

    @BindView(R.id.rcv_menu_category)
    RecyclerView mRcvMenuCategory;

    private CategoryAdapter mAdapter;

    private List<CategoryInfo> mCategories;
    private int mCurrentPosition = 0;
    private LinearLayoutManager mManager;

    public static CategoryListFragment getInstance(List<CategoryInfo> categories) {

        Bundle args = new Bundle();
        args.putParcelableArrayList(ARG_CATEGORY, (ArrayList<? extends Parcelable>) categories);

        CategoryListFragment fragment = new CategoryListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        Bundle args = getArguments();
        if (args != null) {
            mCategories = args.getParcelableArrayList(ARG_CATEGORY);
        }
        CLog.i("size = =======" + mCategories.size());
        mManager = new LinearLayoutManager(mActivity);
        mRcvMenuCategory.setLayoutManager(mManager);
        mAdapter = new CategoryAdapter(mActivity);
        mRcvMenuCategory.setAdapter(mAdapter);
        mAdapter.setDatas(mCategories);

        mAdapter.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, RecyclerView.ViewHolder vh, int position) {
                showContent(position);
            }
        });

        // 恢复数据
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(SAVE_STATE_POSITION);
            mAdapter.setItemChecked(mCurrentPosition);
        } else {
            mCurrentPosition = 0;
            mAdapter.setItemChecked(0);
        }
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_category_list;
    }


    private void showContent(int position) {
        if (position == mCurrentPosition) {
            return;
        }
        mAdapter.setItemChecked(position);

        // 把点击的item移动到menu的中间位置
        int firstPosition = mManager.findFirstVisibleItemPosition();
        int lastPosition = mManager.findLastVisibleItemPosition();
        int middlePosition = (lastPosition - firstPosition) / 2 + firstPosition;
        int top;
        if (position > middlePosition) {
            top = mRcvMenuCategory.getChildAt(position - middlePosition).getTop();
            mRcvMenuCategory.smoothScrollBy(0, top);
        } else {
            top = mRcvMenuCategory.getChildAt(middlePosition - position).getTop();
            mRcvMenuCategory.smoothScrollBy(0, -top);
        }

        mCurrentPosition = position;
        // 点击左边的menu列表切换不同的右边内容区域
        if (mCategories != null) {
            CLog.i("categories_position ================== " + position);
            int type = mCategories.get(position).getType();
            int classifyId = mCategories.get(position).getClassifyId();
            int activityId = mCategories.get(position).getActivityId();

            GoodsListFragment fragment = GoodsListFragment.getInstance(type, type == 0 ? classifyId : activityId);
            ((GoodsCategoryFragment) getParentFragment()).switchContentFragment(fragment);
        }
    }

    /**
     * 保存数据
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SAVE_STATE_POSITION, mCurrentPosition);
    }

    @Override
    protected void initToolbarHere() {
    }

}
